//商品
var product = {
	id:"",
	course:null,
	carData:""
};

//商品详情
product.detail = function(){
	var args = getQueryStringArgs();
	product.id = args["id"];
	var ajax = new $ax(Feng.ctxPath+"/course/getCourseDetail", function(data){
        Feng.log("查询详情成功");
        if (data.success){
            product.course = data.obj.course;    
			
            //头部图片
            var picArry = [];
            for(var i=0; i<4; i++){
            	var picName = product.course["picture"+i];
            	if(picName != null)
            	picArry.push(Feng.ctxPath+"/file/getImage/"+picName);
            }
            var bannerHtml = template("bannerTmpl",picArry);
            $(".aui-banner-content").html(bannerHtml);
            
            //课程基础信息
            //价格过滤器
            template.helper('coursePriceFormat', function (inp) {
                if(inp!==""){
                    return numeral(inp).format('0,0.00');
                }
            });
            
            //上课时间
            template.helper('imgFormat', function (inp) {
				if(inp){
				    return Feng.ctxPath+"/file/getImage/"+inp;
				}
				else{
//				   return Feng.ctxPath+"/file/getImage/logo_b.png";
				   return "themes/img/pd/photo.jpg";
				}
		    });
    
            
            var detailHtml = template("detailTmpl",data.obj);
            $(".aui-banner-content").after(detailHtml);
        }
    },function(data){
        Feng.error("查询详情失败!");
    });
    ajax.set("id",product.id);
    ajax.start();
}

//加入购物车
function shoppingCart(){
	//判断课程是否添加
	var index = product.carData.findIndex(function (item) {
	    return item.id == product.id;
	});
	
	if(index === -1){
		Feng.success("加入购物车成功");
		product.carData.push(product.course);		
	}
	else{
		Feng.info("购物车中已添加此课程");
	}
//	var courseTotal = product.carData.length;
//	$(".aui-f-p-icon .num").text(courseTotal);	
	localStorage.setItem("carData",JSON.stringify(product.carData));
	
	product.carLength();
}

//购物车课程数量
product.carLength = function(){
	product.carData = localStorage.getItem("carData");
	if(product.carData!==null)
		product.carData = JSON.parse(product.carData);
	else
		product.carData = [];
		
	if(product.carData.length>0){
		$(".aui-f-p-icon .num").text(product.carData.length);
		$(".aui-f-p-icon .num").show();		
	}		
	else{
		$(".aui-f-p-icon .num").hide();
	}
			
}


//立即购买,保存订单
function buy(){
	var arry = [product.course];
	var courseData = JSON.stringify(arry);
	localStorage.setItem("courseData",courseData)
	//立即购买
	window.location.href="order.html";
}

$(function(){	
	product.detail();	
	product.carLength();
});
